package com.ruoyi.ucar.util;


import com.ruoyi.common.utils.StringUtils;

import java.util.HashMap;

public class SqlProviderUtil {

public String createSqlOfMobileQueryOrderByCond(HashMap<String,Object> map) {
       StringBuilder sql=new StringBuilder("SELECT " +
               "  o.id as orderId, " +
               "  o.order_uuid as orderUuid, " +
               "  o.order_code as orderCode, " +
               "  o.order_status as orderStatus, " +
               "  o.start_datetime as orderTime, " +
               "  o.pay_status as payStatus, " +
               "  o.order_type AS orderType, " +
               "  o.order_failure_datetime as orderFailureDatetime, " +
               "  o.total_real_price as totalRealPrice, " +
               "  o.total_price as totalPrice, " +
               "  o.total_purchasing_price as totalPurchasingPrice, " +
               "  o.total_return_price as totalReturnPrice, " +
               "  o.is_return_price as isReturnPrice, " +
               "  o.return_price_date as returnPriceDate, " +
               "  o.unread_flag as unreadFlag, " +
               "  p.product_type as productType, " +
               "  op.order_product_data_uuid as orderProductDataUuid, " +
               "  op.queryreport_order_id as queryreportOrderId, " +
               "  p.product_name as productName, " +
               "  op.vin as vin " +
               "FROM " +
               "  t_order o " +
               "  LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping " +
               "  left join t_product p on op.product_uuid_mapping=p.product_uuid "+
               " where o.delete_flag=#{deleteFlag} and op.delete_flag=#{deleteFlag} and p.delete_flag=#{deleteFlag} ");
//                if(StringUtils.isNotNull(map.get("lastMaxId"))){
//                    sql.append(" and o.id >#{lastMaxId}");
//                }
                if(StringUtils.isNotNull(map.get("productUuid"))){
                   sql.append(" and p.product_uuid =#{productUuid} ");
                }
                if(StringUtils.isNotNull(map.get("customerUuid"))){
                     sql.append(" and o.customer_uuid_mapping =#{customerUuid} ");
                }
                if(StringUtils.isNotNull(map.get("orderUuid"))){
                      sql.append(" and o.order_uuid =#{orderUuid} ");
                }
                    if (StringUtils.isNotNull(map.get("orderStatus"))&&!(Boolean)map.get("isHistory")) {
                        String orderStatus = map.get("orderStatus").toString();
                        if (StringUtils.isNotEmpty(orderStatus) && "invalid".equals(orderStatus)) {
                            sql.append(" and o.order_status =#{orderStatus}");
                        }
                        if (StringUtils.isNotEmpty(orderStatus) && !"invalid".equals(orderStatus)) {
                            sql.append(" and o.order_status not in ('invalid','init') ");
                        }
                    }


                sql.append(" and o.order_status !='init'  ");
                if(StringUtils.isNotNull(map.get("vin"))&&StringUtils.isNotEmpty(map.get("vin").toString())){
                    sql.append(" and op.vin like CONCAT('%',#{vin},'%')");
                }
                if(StringUtils.isNotNull(map.get("startDate"))&&StringUtils.isNotNull(map.get("endDate"))){
                    sql.append(" and o.created_time >= #{startDate} and o.created_time <= #{endDate}");
                }
//               sql.append("ORDER BY o.id DESC  LIMIT #{pageSize}");
               sql.append("ORDER BY o.id DESC  ");
               return sql.toString();
     }

    public String createSqlOfQueryOrderAndProductByCond(HashMap<String,Object> map) {
           String sql="SELECT " +
                   " o.agency_fee AS agencyFee, " +
                   " o.total_return_price AS totalReturnPrice, " +
                   " op.upload_file_main_uuid AS uploadFileMainUuid, " +
                   " op.vehicle_brand AS vehicleBrand, " +
                   " op.vin AS vin, " +
                   " op.order_product_data_uuid AS orderProductDataUuid, " +
                   " op.queryreport_file_main_uuid AS queryreportFileMainUuid, " +
                   " op.queryreport_order_id AS queryreportOrderId, " +
                   " p.product_uuid AS productUuid,"+
                   " p.product_type AS productType, " +
                   " p.second_agency_fee_ratio AS secondAgencyFeeRatio,  " +
                   " p.product_status AS productStatus, " +
                   " p.store_number AS storeNumber,  " +
                   " c.parent_customer_uuid AS parentCustomerUuid, " +
                   " c.customer_uuid AS customerUuid, " +
                   " c.sign_file_main_uuid AS signFileMainUuid " +
                   "FROM " +
                   " t_order o " +
                   " LEFT JOIN t_order_product_data op ON o.order_uuid = op.order_uuid_mapping  " +
                   " LEFT JOIN t_customer c ON o.customer_uuid_mapping = c.customer_uuid  " +
                   " LEFT JOIN t_product p ON op.product_uuid_mapping = p.product_uuid  " +
                   "WHERE " +
                   " 1=1 AND op.delete_flag = #{deleteFlag}  " +
                   " AND o.delete_flag = #{deleteFlag}"+
                   " AND c.delete_flag = #{deleteFlag}"+
                   " AND p.delete_flag = #{deleteFlag}"+
                   " AND op.order_uuid_mapping = #{orderUuid}  " ;
                  return sql;
    }



    public String createSqlOfQueryOrderCustomByCond(HashMap<String,Object> map) {
        String sql="SELECT " +
                " c.parent_customer_uuid AS parentCustomerUuid, " +
                " o.agency_fee AS agencyFee, " +
                " o.total_return_price AS totalReturnPrice, " +
                " c.customer_uuid AS customerUuid " +
                "FROM " +
                " t_order o " +
                 " LEFT JOIN t_customer c ON o.customer_uuid_mapping = c.customer_uuid  " +
                 "WHERE " +
                " 1=1 AND o.delete_flag = #{deleteFlag}  " +
                " AND c.delete_flag = #{deleteFlag}"+
                " AND o.order_uuid = #{orderUuid}  " ;
        return sql;
    }

}
